package com.sugar.psych.assessment.module.business.service.configrisklevel;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.psych.assessment.module.business.controller.admin.configrisklevel.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.configrisklevel.ConfigRiskLevelDO;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;

import com.sugar.psych.assessment.module.business.dal.mysql.configrisklevel.ConfigRiskLevelMapper;

import static com.sugar.psych.assessment.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.psych.assessment.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.psych.assessment.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.psych.assessment.module.business.enums.ErrorCodeConstants.*;

/**
 * 风险等级配置 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class ConfigRiskLevelServiceImpl implements ConfigRiskLevelService {

    @Resource
    private ConfigRiskLevelMapper configRiskLevelMapper;

    @Override
    public Long createConfigRiskLevel(ConfigRiskLevelSaveReqVO createReqVO) {
        Integer minVal = createReqVO.getMinVal();
        Integer maxVal = createReqVO.getMaxVal();
        if (minVal > maxVal)
            throw exception(CONFIG_RISK_LEVEL_MIN_NOT_GE_MAX);

        // 插入
        ConfigRiskLevelDO configRiskLevel = BeanUtils.toBean(createReqVO, ConfigRiskLevelDO.class);
        configRiskLevel.setId(null);
        configRiskLevelMapper.insert(configRiskLevel);
        // 返回
        return configRiskLevel.getId();
    }

    @Override
    public void updateConfigRiskLevel(ConfigRiskLevelSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigRiskLevelExists(updateReqVO.getId());
        /*// 校验重复
        Long count = configRiskLevelMapper.selectCount(Wrappers.<ConfigRiskLevelDO>lambdaQuery()
                .eq(ConfigRiskLevelDO::getRiskLevel, updateReqVO.getRiskLevel())
                .ne(ConfigRiskLevelDO::getId, updateReqVO.getId()));
        if (count > 0)
            throw exception(CONFIG_RISK_LEVEL_IS_EXISTS);*/

        Integer minVal = updateReqVO.getMinVal();
        Integer maxVal = updateReqVO.getMaxVal();
        if (minVal > maxVal)
            throw exception(CONFIG_RISK_LEVEL_MIN_NOT_GE_MAX);

        // 更新
        ConfigRiskLevelDO updateObj = BeanUtils.toBean(updateReqVO, ConfigRiskLevelDO.class);
        configRiskLevelMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigRiskLevel(Long id) {
        // 校验存在
        validateConfigRiskLevelExists(id);
        // 删除
        configRiskLevelMapper.deleteById(id);
    }

    @Override
        public void deleteConfigRiskLevelListByIds(List<Long> ids) {
        // 校验存在
        validateConfigRiskLevelExists(ids);
        // 删除
        configRiskLevelMapper.deleteByIds(ids);
        }

    private void validateConfigRiskLevelExists(List<Long> ids) {
        List<ConfigRiskLevelDO> list = configRiskLevelMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_RISK_LEVEL_NOT_EXISTS);
        }
    }

    private void validateConfigRiskLevelExists(Long id) {
        if (configRiskLevelMapper.selectById(id) == null) {
            throw exception(CONFIG_RISK_LEVEL_NOT_EXISTS);
        }
    }

    @Override
    public ConfigRiskLevelDO getConfigRiskLevel(Long id) {
        return configRiskLevelMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigRiskLevelDO> getConfigRiskLevelPage(ConfigRiskLevelPageReqVO pageReqVO) {
        return configRiskLevelMapper.selectPage(pageReqVO);
    }

}